let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cursor: 0,
    hideAdd: 0,
    ismodify: false,
    imgUrl: [],
    content: '',
    num: 0,
    imgUrls: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(e) {
    let that = this
    app.AjaxRequest('get', {
      'content-type': 'application/json',
      'Authorization': 'Bearer ' + app.Getopenid()
    }, '/repair/add', {}, function (res) {
      if (res.code == 0) {
        that.setData({
          parkName: wx.getStorageSync('parkName'),
          member: res.data
        })
      }
    })
  },
  bindTextAreaBlur(e) {
    this.setData({
      content: e.detail.value,
      'member.content': e.detail.value
    })
  },
  headerInput(e) {
    var value = e.detail.value
    var name = e.currentTarget.dataset.name
    this.setData({
      ["member." + e.currentTarget.dataset.name]: value
    })
  },

  previewImg(e) {
    //获取当前图片的下标
    var index = e.currentTarget.dataset.index
    //所有图片
    var imgs = this.data.imgUrl
    console.log(imgs[index])
    wx.previewImage({
      //当前显示图片
      current: imgs[index],
      //所有图片
      urls: imgs
    })
  },
  chooseimage: function () {
    var that = this
    let imgUrl = that.data.imgUrl
    var num = 9 - imgUrl.length
    wx.chooseImage({
      count: num, // 默认9 
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有 
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有 
      success: function (res) {
        //把每次选择的图push进数组
        for (let i = 0; i < res.tempFilePaths.length; i++) {
          imgUrl.push(res.tempFilePaths[i])
        }
        if (imgUrl.length == 9) {
          that.setData({
            hideAdd: 1
          })
        } else {
          that.setData({
            hideAdd: 0
          })
        }
        that.setData({
          imgUrl
        })
      }
    })
  },
  delImg(e) {
    let index = e.currentTarget.dataset.index
    let imgUrl = this.data.imgUrl
    imgUrl.splice(index, 1)
    this.setData({
      imgUrl
    })
  },
  modify() {
    var ismodify = this.data.ismodify
    this.setData({
      ismodify: ismodify == true ? false : true
    })
  },
  apply() {
    let that = this
    let member = that.data.member
    var img = []
    if (!member.content) {
      wx.showToast({
        icon: 'none',
        title: '请输入问题和建议',
      })
      return false
    }
    if (that.data.imgUrl.length > 0) {
      var num = 0
      that.data.imgUrl.forEach(item => {
        app.Upload(item, function (res) {
          if (res.code == 0) {
            img.push({
              name: res.fileName,
              url: res.url,
              uid: num
            })
            that.setData({
              imgUrls: img
            })
            num++
          }
          if (num == that.data.imgUrl.length) {
            that.savefun()
          }
        })
      })
    } else {
      that.savefun()
    }
  },

  savefun() {
    let that = this
    let member = that.data.member
    app.AjaxRequest('POST', {
      'content-type': 'application/json',
      'Authorization': 'Bearer ' + app.Getopenid()
    }, '/suggestion/save', {
      content: that.data.content,
      images: that.data.imgUrls
    }, function (res) {
      if (res.code == 0) {
        wx.showToast({
          title: '提交成功',
          icon: 'none',
          success: (res) => {
            wx.navigateBack()
          }
        })
      }
    })
  }
})